﻿module Sustainalytics.EsgRatings.Admin {

    import Common = Sustainalytics.Common;
    import CommonAdmin = Sustainalytics.Common.Admin;

    export class WeightMatricesService implements IWeightMatricesService {

        private $q: ng.IQService;
        private $http: ng.IHttpService;
        private notificationFactory: Common.INotificationFactory;
        private staticObjectsAdmin: CommonAdmin.IStaticObjects;
        private $upload: any;

        constructor(
            $q: ng.IQService,
            $http: ng.IHttpService,
            notificationFactory: Common.INotificationFactory,
            staticObjectsAdmin: CommonAdmin.IStaticObjects,
            $upload) {

            this.$q = $q;
            this.$http = $http;
            this.notificationFactory = notificationFactory;
            this.staticObjectsAdmin = staticObjectsAdmin;
            this.$upload = $upload;
        }


        //required for 5191 - upload multiple matrices for an account in 1 day
        public listScheduledWeightMatricesByAccountId(accountId: string): ng.IPromise<WeightMatrix[]> {
            var ctx = this;
            var def = this.$q.defer();

            var promise = this.$http({
                method: "PATCH",
                url: "/esgratingserv/api/v1/esgratings/weightmatrices/scheduledWM/" + accountId,
                data: { }
            });

            promise
                .success(function (data) {
                def.resolve(data);
            })
                .error(function (data, status, headers, config) {
                def.reject({ data: data, status: status, headers: headers, config: config });
            });

            return def.promise;
        }

        public listWeightMatricesByAccountId(accountId: string, isFocusedRatings : boolean): ng.IPromise<WeightMatrix[]> {
            var ctx = this;
            var def = this.$q.defer();

            var promise = ctx.$http.get("/esgratingserv/api/v1/esgratings/weightmatrices?" + $.param({ accountId: accountId }));

            promise
                .success(function (data) {
                def.resolve(data);
            })
                .error(function (data, status, headers, config) {
                def.reject({ data: data, status: status, headers: headers, config: config });
            });

            return def.promise;
        }

        public getMatricesSlotsPerPlatform(): ng.IPromise<number> {
            var ctx = this;
            var def = this.$q.defer();

            var promise = ctx.$http.get("/esgratingserv/api/v1/esgratings/weightmatrices/slots");

            promise
                .success(function (data) {
                def.resolve(data);
            })
                .error(function (data, status, headers, config) {
                def.reject({ data: data, status: status, headers: headers, config: config });
            });

            return def.promise;
        }

        public updateCustomWeightMatrix(matrix: WeightMatrix): ng.IPromise<WeightMatrix> {
            var ctx = this;
            var def = this.$q.defer();
            var url = '/esgratingserv/api/v1/esgratings/weightmatrices';
            var promise = this.$http.put(url, matrix);
            promise.success(function (data) { def.resolve(data); }).error(function (err) {
                ctx.notificationFactory.error("", "Error updating custom weight matrix.");
                def.reject();
            })
            return def.promise;
        }

        public deleteCustomWeightMatrix(id: string): ng.IPromise<any> {
            var ctx = this;
            var def = this.$q.defer();
            var url: string = '/esgratingserv/api/v1/esgratings/weightmatrices/' + id;
            var promise: ng.IHttpPromise<{}> = this.$http.delete(url);
            promise.success(function (data) { def.resolve(data); }).error(function (err) {
                ctx.notificationFactory.error("", "Error removing custom weight matrix.");
                def.reject();
            })
            return def.promise;
        }

    }


    var mod = angular.module(esgRatingsAdminModuleName);

    // register service with EsgRatings Angular module
    mod.service('WeightMatricesService',
        ['$q', '$http', 'notificationFactory', 'staticObjectsAdmin', '$upload', WeightMatricesService]);
}